#@auth.requires_login()
def index():
    operations = { "COMMENT" : "commented on",
                    "ADD": "added",
                    "BORROW" : "borrowed",
                    "REQUEST" : "requested",
                    "REMOVED" : "removed",
                    "MODIFY": "modified",
                    "CANCEL": "cancelled"}
    things = { "BOOK" : "the book",
                "USER": "the user"}

    newBooks = db().select(db.books.ALL, orderby=~db.books.submissionDate, limitby=(0,10))
    recentLog = db(db.users.id==db.log.byUser).select(db.log.ALL,db.users.username, db.users.first_name, db.users.last_name, db.users.id, orderby=~db.log.datetime, limitby=(0,10))
    logs = list()
    for log in recentLog:
        if log.log.onWhat == 'BOOK':
            row = db(db.books.id==log.log.onWho).select(db.books.id, db.books.title)
            myA = A(row[0].title, _href=URL(r=request, c="books", f="book", args=[row[0].id]))
        elif log.log.onWhat == 'USER':
            row = db(db.users.id==log.log.onWho).select(db.users.id, db.users.username, db.users.first_name, db.users.last_name)
            myA = A(row[0].first_name," ", row[0].last_name , " (", row[0].username, ")", _href=URL(r=request, c="users", f="profile", args=[row[0].id]))
        logs.append(DIV(A(log.users.first_name," ",log.users.last_name, "(", log.users.username ,")", _href=URL(r=request, c="users", f="profile", args=[log.users.id]))," ", operations[log.log.operation], " ", things[log.log.onWhat], " ", myA))
    return dict(newBooks=newBooks, recentLog=recentLog, logs=logs)


# # uncomment the following if you have defined "auth" and "crud" in models
def user(): 
    return dict(form=auth())
    
def data(): 
    return dict(form=crud())
    
def download(): 
    return response.download(request,db)
# # tip: use @auth.requires_login, requires_membership, requires_permission
